package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void addFriend(Long userId, Long friendId) {
        // 1.删除二个关系
        // 1-1 你删他
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
        // 1-2 他删你
        mongoTemplate.remove(new Query(Criteria.where("friendId").is(userId).and("userId").is(friendId)), Friend.class);
        // 2.删除对方动态
        // 2-1 你删他
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId)), ConstantUtil.MOVEMENT_FRIEND+userId);
        // 2-2 他删你
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)), ConstantUtil.MOVEMENT_FRIEND+friendId);

        // 3.你加他
        // 3-1 完成好友添加
        Friend friend1 = new Friend();
        friend1.setUserId(userId); // 你的id
        friend1.setFriendId(friendId); // 好友的id
        friend1.setCreated(System.currentTimeMillis());// 添加时间
        mongoTemplate.save(friend1);
        // 3-2 查询好友的动态
        List<Movement> movementList1 = mongoTemplate.find(new Query(Criteria.where("userId").is(friendId)), Movement.class);
        if (CollectionUtil.isNotEmpty(movementList1)) {
            for (Movement movement : movementList1) {
                // 3-3 将他的动态保存到你好友动态表
                FriendMovement friendMovement1 = new FriendMovement(); // movement_friend_99
                friendMovement1.setCreated(movement.getCreated()); // 好友动态发布时间
                friendMovement1.setPublishId(movement.getId()); //  好友动态id
                friendMovement1.setUserId(movement.getUserId()); // 好友用户id
                // 3-4 保存
                mongoTemplate.save(friendMovement1,ConstantUtil.MOVEMENT_FRIEND+userId);
            }
        }

        // 4.他加你
        // 4-1 完成好友添加
        Friend friend2 = new Friend();
        friend2.setUserId(friendId); // 好友的id
        friend2.setFriendId(userId); // 你的id
        friend2.setCreated(System.currentTimeMillis());// 添加时间
        mongoTemplate.save(friend2);
        // 4-2 查询你的动态列表
        List<Movement> movementList2 = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), Movement.class);
        if (CollectionUtil.isNotEmpty(movementList2)) {
            for (Movement movement : movementList2) {
                // 4-3 将你的动态保存到他好友动态表  // movement_friend_2
                FriendMovement friendMovement2 = new FriendMovement();
                friendMovement2.setCreated(movement.getCreated()); // 你动态发布时间
                friendMovement2.setPublishId(movement.getId()); //  你的动态id
                friendMovement2.setUserId(movement.getUserId()); // 你的用户id
                // 4-4 保存
                mongoTemplate.save(friendMovement2,ConstantUtil.MOVEMENT_FRIEND+friendId);
            }
        }
    }

    @Override
    public PageBeanVo findFriendByPage(Long userId, Integer pageNum, Integer pageSize) {
        // 1.构建条件
        Query quer = new Query(Criteria.where("userId").is(userId))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        // 2.查询list
        List<Friend> friendList = mongoTemplate.find(quer, Friend.class);
        // 3.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, 0L, friendList);
    }
}
